package com.zhongwang.mms.module.jiaGongMaterial.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.basic.service.MMaterialDictionaryService;
import com.zhongwang.mms.module.basic.service.TDeptService;
import com.zhongwang.mms.module.jiaGongMaterial.dao.MaterialInDao;
import com.zhongwang.mms.module.jiaGongMaterial.model.JMaterialAllModel;
import com.zhongwang.mms.module.jiaGongMaterial.model.JMaterialInsertModel;
import com.zhongwang.mms.module.jiaGongMaterial.service.MaterialInService;
import com.zhongwang.mms.module.material.dao.CompositeOutcomeDao;
import com.zhongwang.mms.module.material.dao.MOrderDao;
import com.zhongwang.mms.module.material.dao.MOrderDetailDao;
import com.zhongwang.mms.module.material.model.ComplexStorageModel;
import com.zhongwang.mms.module.material.model.CompositeOutcomeModel;
import com.zhongwang.mms.module.material.model.MOrderInsertModel;
import com.zhongwang.mms.module.material.service.MOrderService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 原料出入库 控制层
 * @Author: China.wsg
 * @Date: 2020/04/01 11:23
 */
@Controller
@RequestMapping("/materialInto")
public class MaterialInController extends BaseController {

    @Autowired
    private MOrderService mOrderService;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;
    @Autowired
    private MMaterialDictionaryService mMaterialDictionaryService;
    @Autowired
    private MOrderDao mOrderDao;
    @Autowired
    private MOrderDetailDao mOrderDetailDao;
    @Autowired
    private CompositeOutcomeDao compositeOutcomeDao;
    @Autowired
    private TDeptService deptService;
    @Autowired
    private MaterialInService materialInService;
    @Autowired
    private MaterialInDao materialInDao;

    /**
     * 原料入库 页面
     *
     * @return 页面路径
     */
    @GetMapping("/pageIn")
    public String page(Model model) {
        // 获取登入用户实体
        model.addAttribute("odrMakePerson", ShiroUtils.getUser().getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原材料甲供件库"));
        return "jiaGongMaterial/materialIn/page";
    }

    /**
     * 创建原材料入库单
     *
     * @param jMaterialInsertModel
     * @return
     */
    @ResponseBody
    @PostMapping("/inCreate")
    public JsonResult inCreate(@RequestBody JMaterialInsertModel jMaterialInsertModel) {

        JsonResult jsonResult = new JsonResult();
        String msg = materialInService.saveInOrderModel(jMaterialInsertModel);
        if (msg.equals("单据保存成功！")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 原料出库 页面
     *
     * @return 页面路径
     */
    @GetMapping("/pageOut")
    public String outCreatePage(Model model) {
        // 获取登入用户实体
        model.addAttribute("matType", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialTypeList());
        model.addAttribute("matQuality", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialQualityList());
        model.addAttribute("matSpec", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialSpecList());
        model.addAttribute("odrMakePerson", ShiroUtils.getUser().getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("dept", deptService.findDeptInfo(new TDept()));
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原材料甲供件库"));
        return "jiaGongMaterial/materialOut/page";
    }

    /**
     * 创建原材料出库单
     *
     * @param jMaterialInsertModel
     * @return
     */
    @ResponseBody
    @PostMapping("/outCreate")
    public JsonResult outCreate(@RequestBody JMaterialInsertModel jMaterialInsertModel) {

        JsonResult jsonResult = new JsonResult();
        String msg = materialInService.saveOutOrderModel(jMaterialInsertModel);
        if (!msg.contains("保存失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 查询原料在库明细
     *
     * @param jMaterialAllModel 查询条件
     * @param matTypeIdList       类型Id多选
     * @param matQualityIdList    材质Id多选
     * @param matSpecIdList       规格Id多选
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(JMaterialAllModel jMaterialAllModel, @RequestParam(value = "matTypeIdList[]", required = false) List<String> matTypeIdList,
                           @RequestParam(value = "matQualityIdList[]", required = false) List<String> matQualityIdList,
                           @RequestParam(value = "matSpecIdList[]", required = false) List<String> matSpecIdList,
                           @RequestParam(value = "whiIdList[]", required = false) List<String> whiIdList) {
        startPage();
        if(jMaterialAllModel.getWhiIdListStr()!=null){
            whiIdList=Arrays.asList(jMaterialAllModel.getWhiIdListStr().split(","));
        }
        List<JMaterialAllModel> list = materialInService.findStorage(jMaterialAllModel, matTypeIdList, matQualityIdList, matSpecIdList,whiIdList);
        return jsonResult(list);
    }
    /**
     * 冲单页面 页面
     *
     * @return 页面路径
     */
    @GetMapping("/redOrderDetail")
    public String redOrderDetail() {
        return "material/matWarManage/RedOrderDetail/list";
    }

    /**
     * 获取主单信息
     *
     * @param odrId 主单Id
     * @return
     */
    @ResponseBody
    @GetMapping("/orderMaster")
    public JsonResult getOrderMaster(Integer odrId) {

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(materialInDao.selectMaterialById(odrId));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 获取单据明细（根据主单Id）
     *
     * @param odrId 主单Id
     * @return
     */
    @ResponseBody
    @GetMapping("/orderDetail")
    public JsonResult getOrderDetailById(Integer odrId) {

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(materialInDao.findOrderDetailByPrimaryKey(odrId));
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 获取单据明细（根据检索条件）
     *
     * @param compositeOutcomeModel 检索条件
     * @return
     */
    @ResponseBody
    @PostMapping("/orderDetailInfo")
    public JsonResult getOrderDetailByQuery(CompositeOutcomeModel compositeOutcomeModel) {
        startPage();
        List<CompositeOutcomeModel> list = compositeOutcomeDao.findCompositeOutcome(compositeOutcomeModel);
        return jsonResult(list);
    }

    /**
     * 明细红冲（单条红冲）
     *
     * @param odaId 明细Id
     * @return
     */
    @ResponseBody
    @PostMapping("/redDetailSingle")
    public JsonResult redDetailSingle(Integer odaId) {

        JsonResult jsonResult = new JsonResult();
        String msg = mOrderService.redDetailSingle(odaId);
        if (!msg.contains("红冲失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 明细红冲（批量红冲）
     *
     * @param arr 明细Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/redDetailTogether")
    public JsonResult redDetailTogether(@RequestBody List<Integer> arr) {

        JsonResult jsonResult = new JsonResult();
        String msg = mOrderService.redDetailTogether(arr);
        if (!msg.contains("红冲失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }
}
